package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.LotteryRedisDataVO;
import com.example.demo.entity.TUser;
import com.example.demo.enums.CollectNeedSameTimeEnum;
import com.example.demo.log.DGuanUserResourceParam;
import com.example.demo.log.JsonLog;
import com.example.demo.service.TUserService;
import com.example.demo.util.HttpClientUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Classname DemoController
 * @Description TODO
 * @Date 2019/10/15 10:48
 * @Created by yby
 */
@RestController
@RequestMapping("/yby")
@Slf4j
public class DemoController {

    private static final Logger logger = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    private TUserService tUserService;

    @RequestMapping("/insert")
    public void insertData(@RequestBody TUser tuser, String name) {
        logger.info("tuser:{},name:{}", JSONObject.toJSONString(tuser), name);
        System.out.println("insert ...");
        tUserService.addData(tuser);
    }

    @RequestMapping("/speak")
    public LotteryRedisDataVO speak() {
        /*System.out.println("测试mvc接口");
        LotteryRedisData lotteryRedisData=new LotteryRedisData();
        lotteryRedisData.setCpOdd("10000A");
        lotteryRedisData.setStatus("online");
        lotteryRedisData.setId(123456L);
        lotteryRedisData.setLastUpdateTime(124556L);
        LotteryRedisDataVO vo=new LotteryRedisDataVO();
        BeanUtils.copyProperties(lotteryRedisData, vo);
        vo.setByteStatus(356);
        vo.setByteStatus(1);
        if(vo.getByteStatus() == 1){
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("print1","硬件1未连接");
            jsonObject.put("print2","硬件2未连接");
            jsonObject.put("print","打印机故障");
            vo.setByteStatusDetail(jsonObject.toJSONString());
        }
        System.out.println(vo.toString());

        if(vo.getByteStatus() !=1 && vo.getByteStatus() !=0){
            System.out.println("我不是1哦");
        }*/

        return null;


    }

    @Data
    public static class CustomDiskSearchReq {
        private boolean isFake = false;//注意，请求时字段名传fake
        private Byte dataType;
        private int page = 1;
        private int pageSize = 20;
        private String userId;
    }

    @PostMapping("search")
    public String search(HttpServletRequest servletRequest, @RequestBody CustomDiskSearchReq req) {
        DGuanUserResourceParam dGuanUserResourceParam = new DGuanUserResourceParam();
        dGuanUserResourceParam.setUserId("b8d65ec31d784af98ccc085b59b56145");
        dGuanUserResourceParam.setAppSecret("dgqk");
        dGuanUserResourceParam.setAppId("dgqk");
        dGuanUserResourceParam.setUserId(req.getUserId());
        dGuanUserResourceParam.setPageNum(req.getPage());
        dGuanUserResourceParam.setPageSize(req.getPageSize());
        dGuanUserResourceParam.setTimestamp(System.currentTimeMillis());
        if (req.getDataType() != null) {
            dGuanUserResourceParam.setResourceType(Integer.valueOf(req.getDataType()));
        }
        log.info("CustomDiskFileController--->searchDg--->req:{}", JsonLog.toJson(req));
        log.info("CustomDiskFileController--->searchDg--->dGuanUserResourceParam1:" + JsonLog.toJson(dGuanUserResourceParam));
        log.info("CustomDiskFileController--->searchDg--->dGuanUserResourceParam:{}", JsonLog.toJson(dGuanUserResourceParam));

        return "成功";
    }

    @GetMapping("getToken")
    public String getToken() {
        String sign = "2";
        String timestamp = "2";
        String appId = "33";
        String url = "https://wwwmedia.dg.cn/thirdServiceApi/api/third/getToken";
        String response = HttpClientUtils.doGet("http://wwwmedia.dg.cn/thirdServiceApi/api/third/getToken?sign=" + sign + "&timestamp=" + timestamp + "&appId=" + appId);

        return response;
    }


    public static void main(String[] args) throws InterruptedException {

        /*long start = System.currentTimeMillis();
        Thread.sleep(6000);
        long end =System.currentTimeMillis();
        LOG.info("数字"+(end-start)+"ms");
        long minutes = ((end-start) % (1000 * 60 * 60)) / (1000 * 60);
        System.out.println(minutes);*/
        List<String> list = new ArrayList<>();
        list.add("1111");
        list.add("2222");
        list.add("3333");
        list.add("4444");
        list.add("5555");
       /* DemoController DemoController =new DemoController();
        DemoController.speak(list);
        DemoController.say(list);*/
        // 获取主机/辅机 个数
        Map<String, String> enumMap = CollectNeedSameTimeEnum.getKeysAndDeviceCode();
        Iterator<Map.Entry<String, String>> iterator = enumMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> entry = iterator.next();
            // 通用的 需要根据个数进行转换 如主机/辅机
            System.out.println("entry.getValue()=" + entry.getValue());
            System.out.println("entry.getKey()=" + entry.getKey());
        }
        System.out.println(enumMap.toString());

    }

    @Async
    public void say(List<String> list) {
        System.out.println("say=" + list.toString());
    }

    @Async
    public void speak(List<String> list) {

        list.remove("5555");
        list.add("6666");
        System.out.println("speak=" + list);
    }


}